﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// ReSharper disable once CheckNamespace
namespace com.kmer.Helper {
	/// <summary>
	/// 数学和数字有关系的方法
	/// </summary>
	public static class MathHelper {
        /// <summary>
        /// 是否在两个数字之间（min &lt; value &lt; max），如果min大于max会自动交换
        /// </summary>
        /// <param name="theValue"></param>
        /// <param name="min">小值（不含）</param>
        /// <param name="max">大值（不含）</param>
        /// <returns></returns>
        public static bool IsBetween(this int theValue, int min, int max) {
			if (min > max) {
				Swap(ref min, ref max);
			}
			return theValue > min && theValue < max;
		}
		/// <summary>
		/// 返回在给定的大小值之间的数值，不会修改theValue
		/// </summary>
		/// <param name="theValue"></param>
		/// <param name="min"></param>
		/// <param name="max"></param>
		/// <returns></returns>
		public static int MakeInRange(this int theValue, int min, int max) {
			if (min > max) {
				Swap(ref min, ref max);
			}
			return theValue < min
				? min
				: (theValue > max ? max : theValue);
		}
		/// <summary>
		/// 交换数值
		/// </summary>
		/// <param name="v1"></param>
		/// <param name="v2"></param>
		public static void Swap(ref int v1, ref int v2) {
			var tmp = v1;
			v1 = v2;
			v2 = tmp;
		}
	}
}
